package com.hoshiicloud.payment.feign;

import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.payment.feign.fallback.UploadServiceFeignFallbackFactory;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: hoshiicloud
 * @description: 测试
 * @author: Mr.wu
 * @create: 2019-06-04 16:28
 **/
@FeignClient(name = "hoshiicloud-fileserver", configuration = UploadService.MultipartSupportConfig.class, fallbackFactory = UploadServiceFeignFallbackFactory.class)
public interface UploadService {

    @PostMapping(value = "/file/test")
    Response test();

    /**
     * @param bucketName
     * @param objectKey
     * @param fileName
     * @param folder
     * @param file
     * @return com.hoshiicloud.common.rpc.Response
     * @author: Mr.wu
     * @descripton: 上传文件
     * @date: 17:00 2019/6/5
     */
    @PostMapping(value = "/file/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response fileUpload(@RequestParam("bucketName") String bucketName,
            @RequestParam("objectKey") String objectKey, @RequestParam("fileName") String fileName,
            @RequestParam("folder") String folder,
            @RequestParam("privateOwned") Boolean privateOwned, @RequestPart MultipartFile file);

    public class MultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}
